<template>
<div class="login-wrap">
  <div class="ms-title">Web Nmap管理系统</div>
  <div class="ms-login">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" placeholder="密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
      </el-form-item>
      <el-form-item class="center_item">
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import Vue from 'vue'
import qs from 'qs'

import axios from 'axios'

export default {
  data: function() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      const self = this;
      const store = this.store;
      self.$refs[formName].validate((valid) => {
        if (valid) {
          //console.log(Vue.prototype.$http);

          var data = {
            username: self.ruleForm.username,
            password: self.ruleForm.password
          };

/*
          var data = new URLSearchParams();
          data.append('username', self.ruleForm.username);
          data.append('password', self.ruleForm.password);
          */

          this.axios({
              method: 'POST',
              url: 'new_login',
              data: data
            })
            .then(function(response) {
              // url: 'http://localhost:5000/new_login',
              console.log("response:", response);
              if (response.status == 200) {
                var data = response.data;
                var err_code = data.err_code;
                if (err_code != 0) {
                  Vue.prototype.store.dispatch("setUser", "");
                  Vue.prototype.store.dispatch("setRoles", "");
                  Vue.prototype.store.dispatch("setToken", "");
                  localStorage.setItem('username', "");
                  localStorage.setItem('roles', "");
                  localStorage.setItem('token', "");
                  return false;
                } else {
                  var email = data.res.email;
                  var roles = data.res.roles;
                  var token = data.res.token;
                  Vue.prototype.store.dispatch("setUser", email);
                  Vue.prototype.store.dispatch("setRoles", roles);
                  Vue.prototype.store.dispatch("setToken", token);
                  localStorage.setItem('username', email);
                  localStorage.setItem('roles', roles);
                  localStorage.setItem('token', token);
                  self.$router.push('/home');
                }
              } else {
                Vue.prototype.store.dispatch("setUser", "");
                Vue.prototype.store.dispatch("setRoles", "");
                Vue.prototype.store.dispatch("setToken", "");
                localStorage.setItem('username', "");
                localStorage.setItem('roles', "");
                localStorage.setItem('token', "");
                return false;
              }
            })
            .catch(function(error) {
              console.log("error:", error);
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
}

.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;

}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 160px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}

.center_item {
  text-align: center;
}
</style>
